#include "StdAfx.h"
#include "ShaderProgram.h"

namespace banknamespace
{
	ShaderProgram::ShaderProgram(void)
	{
	
	}

	ShaderProgram::~ShaderProgram(void)
	{
		glDeleteShader(v);
		glDeleteShader(f);

		glDeleteProgram(p);
	}

	GLint ShaderProgram::getProgramId()
	{
		return p;
	}

	void ShaderProgram::createProgram(std::string vsPath, std::string psPath) 
	{
		GLint success;
		char *vs,*fs;

		v = glCreateShader(GL_VERTEX_SHADER);
		f = glCreateShader(GL_FRAGMENT_SHADER);	

		vs = textFileRead(vsPath.c_str());
		fs = textFileRead(psPath.c_str());

		const char * vv = vs;
		const char * ff = fs;

		glShaderSource(v, 1, &vv,NULL);
		glShaderSource(f, 1, &ff,NULL);

		free(vs);free(fs);

		glCompileShader(v);
		glGetShaderiv(v, GL_COMPILE_STATUS, &success);

		if (!success)
		{
			GLint length;
			GLchar log;
			char *infoLog;

			glGetShaderiv (v, GL_INFO_LOG_LENGTH, &length);
			//glGetShaderInfoLog (v, length, &length, &log);

			infoLog = (char *)malloc(length);
			glGetShaderInfoLog(v, length, &length, infoLog);

			wchar_t* w_infoLog = CharToWChar( infoLog );

			MessageBox( NULL, w_infoLog, L"Error", 0 );
			free(infoLog);

			glDeleteShader( v );
		}

		glCompileShader(f);
		glGetShaderiv(f, GL_COMPILE_STATUS, &success);
		
		if (!success)
		{
			GLint length;
			GLchar log;
			char *infoLog;

			glGetShaderiv (f, GL_INFO_LOG_LENGTH, &length);
			//glGetShaderInfoLog (f, length, &length, &log);

			infoLog = (char *)malloc(length);
			glGetShaderInfoLog(f, length, &length, infoLog);

			wchar_t* w_infoLog = CharToWChar( infoLog );

			MessageBox( NULL, w_infoLog, L"Error", 0 );
			free(infoLog);

			glDeleteShader( f );
		}

		if (success)
		{
			p = glCreateProgram();

			glAttachShader(p,v);
			glAttachShader(p,f);




			//glLinkProgram(p);


		}
		else
		{
			p = -1;
		}
	}
}

